package restaurant.behaviours.master;

import java.util.Hashtable;

import jade.core.behaviours.CyclicBehaviour;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import restaurant.agents.MasterWaiterAgent;
import restaurant.core.CustomizedPerformative;

public class MasterCallBehaviour extends CyclicBehaviour {
	
	private static final long serialVersionUID = 5131056031578649484L;
	private MasterWaiterAgent agent;

	public MasterCallBehaviour(MasterWaiterAgent masterWaiterAgent) {
		this.agent = masterWaiterAgent;
	}

	@Override
	public void action() {
		MessageTemplate messageTemplate = MessageTemplate.MatchPerformative(CustomizedPerformative.CALL);
		ACLMessage message = myAgent.receive(messageTemplate);
			
		if(message != null) {
			//Called from <table>
			int table = Integer.parseInt(message.getContent());
			
			synchronized (this.agent.calledTime) {
				this.agent.calledTime.add(table, System.currentTimeMillis());
			}
			
			Hashtable<Integer, Boolean> tmp = new Hashtable<Integer, Boolean>();
			tmp.put(table, false);
			
			System.out.println("MasterWaiterAgent: Devo mandare un cameriere al tavolo " + table);
			
			synchronized (this.agent.restaurant.calls) {
				this.agent.restaurant.calls.add(tmp);
			}
		}
	}
}